import { getObject, i18n } from '@app-nextmesh/common/utils';
import {
  autoCreateChild,
  getAmcaxProp,
  getLabel,
  getSelfEditKey,
  isChildCreatable,
  makeDefaultForm,
} from '../../components/Forms/components/utils';
import { IMenuItem } from '../../components/types';
import { AmcaxProp } from '../../types';
import {
  CalculiX,
  CFDC,
  GaloisConfig,
  RFM3D,
  StructuralN,
  ILWConfig,
} from './SloverConfig';
import { deletionMenu, separatorMenu } from './commonMenu';

// 仿真类型
const STRUCTURAL = 'Structural';
const CFD = 'CFD';
const AIRFOILCFD = 'AIRFOILCFD';
const SimuTypeOptions = [
  {
    label: i18n.t(STRUCTURAL),
    value: STRUCTURAL,
  },
  {
    label: i18n.t(CFD),
    value: CFD,
  },
  {
    label: i18n.t(AIRFOILCFD),
    value: AIRFOILCFD,
  },
];

// 定义不同仿真类型，对应的边界条件和研究参数
const SimulationMenus = {
  Structural: {
    结构力学N: StructuralN,
    Calculix: CalculiX,
    RFM3D: RFM3D,
  },
  CFD: {
    流体力学C: CFDC,
  },
  AIRFOILCFD: {
    Galois: GaloisConfig,
    ILW: ILWConfig,
  },
};
const makeSimuConditionMenuItems = (jsonPath: string) => {
  // jsonPath 格式：simuCondition-CFD-流体力学C-boundaries
  const names = jsonPath.split('-');
  names.splice(0, 1);
  const formFormat = getObject(SimulationMenus, names.join('.'));
  const menuItems: IMenuItem[] = [];
  const amcaxProp = getAmcaxProp(formFormat);
  if (amcaxProp.childCreatable) {
    Object.keys(formFormat)
      .filter((key) => key !== AmcaxProp) // 过滤掉保留字段
      .forEach((paramKey) => {
        const label = getLabel(paramKey);
        menuItems.push({ label: label, value: paramKey });
      });
  }
  if (amcaxProp.deletable) {
    if (menuItems.length > 0) {
      menuItems.push(separatorMenu);
    }
    menuItems.push(deletionMenu);
  }
  return menuItems;
};
const getSimulationFormFormat = (jsonPath: string) => {
  // jsonPath 格式：仿真类型.插件类型.JSONFormFormatKeys
  // 比如：CFD.流体力学C.contacts.SurfaceInteraction
  const formFormat = getObject(SimulationMenus, jsonPath);
  return formFormat;
};
const getSolveTypeFormat = (name: string) => {
  // name 仿真类型名称，比如: Structural/CFD等
  return SimulationMenus[name]['SolveType'] ?? {};
};

/**
 * 根据仿真类型和插件类型，由 JSON 生成默认的条件
 * @returns
 */
const getDefaultCondition = (jsonPath: string) => {
  const formFormat = getObject(SimulationMenus, jsonPath);
  if (formFormat) {
    // 获取formFormat 的第一层
    const keys = Object.keys(formFormat);
    const condition = {};
    const initCondition = (condition, key, formFormat) => {
      const value = formFormat[key];
      if (isChildCreatable(value)) {
        // 能够添加子节点的项，直接初始化为空数组
        condition[key] = [];
      } else if (autoCreateChild(value)) {
        // 是否自动创建子项，和添加子节点的项互斥
        // TODO 目前支持的的CFDC的solver，后续应该也会重构
        const subKeys = Object.keys(value).filter((k) => k !== AmcaxProp);
        condition[key] = {};
        const children = condition[key];
        for (let i = 0; i < subKeys.length; i++) {
          const k = subKeys[i];
          const valueK = value[k];
          if (valueK[AmcaxProp]) {
            initCondition(condition[key], k, value);
          } else {
            children[k] = makeDefaultForm(value[k]);
          }
        }
      }
      const selfEditKey = getSelfEditKey(value);
      if (selfEditKey !== undefined) {
        if (condition[key] === undefined) {
          condition[key] = {};
        }
        const defaultForm = makeDefaultForm(
          selfEditKey ? value[selfEditKey] : value,
        );
        if (selfEditKey) {
          condition[key][selfEditKey] = defaultForm;
        } else {
          condition[key] = defaultForm;
        }
      }
    };
    //　TODO 统一处理逻辑，初始化需要默认值的项
    keys.forEach((key) => {
      initCondition(condition, key, formFormat);
    });
    return condition;
  } else {
    return {};
  }
};

export {
  getDefaultCondition,
  getSimulationFormFormat,
  getSolveTypeFormat,
  makeSimuConditionMenuItems,
  SimuTypeOptions,
};
